#include "localtcpser.hpp"
#include "protocol.hpp"
#include "netcal.hpp"

// #define UNIQUE_TCPSER std::unique_ptr<Local_TcpSer::TcpServer>
// #define MAKE_UNIQUE_TCPSER std::make_unique<Local_TcpSer::TcpServer>
// #define UNIQUE_SOCKET std::shared_ptr<Local_Sock_Mod::Socket>
// #define LOCAL_INETADDR Local_Inetaddr::Inetaddr

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        SerErr();
        exit(ERR_SER);
    }
    uint16_t tcp_port = std::stoi(argv[1]);

    // 启动业务
    NetCal ma_netcal;

    // 启动协议
    Protocol ma_pro([&ma_netcal](Request &req) -> Response
                    { return ma_netcal.NetCalculation(req); });

    // 创建服务器
    // UNIQUE_TCPSER ma_tcpser = MAKE_UNIQUE_TCPSER(tcp_port,
    //                                              [&ma_pro](UNIQUE_SOCKET &sockfd, LOCAL_INETADDR &client)
    //                                              {
    //                                                  ma_pro.GetRequest(sockfd, client);
    //                                              });

    std::unique_ptr<Local_TcpSer::TcpServer> ma_tcpser = std::make_unique<Local_TcpSer::TcpServer>(tcp_port,
                                                                                                   [&ma_pro](std::shared_ptr<Local_Sock_Mod::Socket> &sockfd, Local_Inetaddr::Inetaddr &client)
                                                                                                   {
                                                                                                       ma_pro.GetRequest(sockfd, client);
                                                                                                   });

    ma_tcpser->start();

    return 0;
}

void test()
{
    Request req;
    Protocol pro(nullptr);
    std::string ss = req.Serialize(10, 20, '%');
    std::string allss = pro.AddHead(ss);
    std::string ss2;
    pro.Decode(allss, &ss2);
    std::cout << ss2;
}
int xxxx()
{
    test();
    return 0;
}
